//
//  YQCheckOtherLogin.m
//  industry
//
//  Created by 王雅强 on 2021/2/20.
//  Copyright © 2021 Haier. All rights reserved.
//

#import "YQCheckOtherLogin.h"
#import "XLUserDefaults.h"
#import "XLLoginRegisterVC.h"
#import "XLNetworking.h"
#import "AppStartUpConfig.h"

@implementation YQCheckOtherLogin



+(void)loginRepuest:(NSString *)code withType:(XLLoginBindType)type andStaffState:(BOOL)state andCurrentVC:(UIViewController *)viewC{
    
    switch (type) {
        case XLLoginBindWeiXin:
            [self weiXinLoginRepuest:code andStaffState:state andCurrentVC:viewC];
            break;
        case XLLoginBindApple:
            [self appleLoginRepuest:code andStaffState:state andCurrentVC:viewC];
            break;
        default:
            break;
    }
    
}


+(void)weiXinLoginRepuest:(NSString *)code andStaffState:(BOOL)state andCurrentVC:(UIViewController *)viewC{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(state) forKey:@"isStaff"];
    [params setValue:code forKey:@"openId"];
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    


    
    [request requestWithMethod:XLHttpMedthTypeGET url:XLWeiXinLoginUrl params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
        
        if ([dict[@"data"][@"isExist"]  isEqual: @1]) {
            //存储用户登录账号
            
            [XLUserDefaults saveLastLoginPhone:dict[@"data"][@"phone"]];
            
            if ([dict[@"data"][@"loginVO"] isKindOfClass:[NSDictionary class]]) {
                [self handleLoginData:dict[@"data"][@"loginVO"] isStaff:state andCurrentVC:viewC];
            }
            
        }else{
            XLLoginRegisterVC *vc = [[XLLoginRegisterVC alloc] init];
            vc.entryType = XLEntryLoginRegisterVCTypeBind;
            vc.isStaff = state;
            vc.openID = code;
            vc.bindType = XLLoginBindWeiXin;
            [viewC.navigationController pushViewController:vc animated:YES ];
        }
 
      
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
        XLLog(@"错误%@,%@",error,dict)
        [XLToast showErrorMessage:[NSString stringWithFormat:@"微信登录错误%@,%@",error,dict]];
       
    }];
    
}

+(void)appleLoginRepuest:(NSString *)code andStaffState:(BOOL)state andCurrentVC:(UIViewController *)viewC{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@(state) forKey:@"isStaff"];
    [params setValue:code forKey:@"appleId"];
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    


    [request requestWithMethod:XLHttpMedthTypeGET url:XLAppleLoginUrl params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
        
        if ([dict[@"data"][@"isExist"]  isEqual: @1]) {
            //存储用户登录账号
            
            [XLUserDefaults saveLastLoginPhone:dict[@"data"][@"phone"]];
            
            if ([dict[@"data"][@"loginVO"] isKindOfClass:[NSDictionary class]]) {
                [self handleLoginData:dict[@"data"][@"loginVO"] isStaff:state andCurrentVC:viewC];
            }
            
        }else{
            XLLoginRegisterVC *vc = [[XLLoginRegisterVC alloc] init];
            vc.entryType = XLEntryLoginRegisterVCTypeBind;
            vc.isStaff = state;
            vc.openID = code;
            vc.bindType = XLLoginBindApple;
            [viewC.navigationController pushViewController:vc animated:YES ];
        }
 
      
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
        XLLog(@"错误%@,%@",error,dict)
        [XLToast showErrorMessage:[NSString stringWithFormat:@"微信登录错误%@,%@",error,dict]];
       
    }];
    
}

+ (void)handleLoginData:(NSDictionary *)dict
                isStaff:(BOOL)isStaff andCurrentVC:(UIViewController *)viewC{
    [XLUserData sharedXLUserData].isHomeFirst = YES;
    [XLUserData sharedXLUserData].isMessageFirst = YES;
    [XLUserData sharedXLUserData].isApproveFirst = YES;
    [XLUserData sharedXLUserData].isUserCenterFirst = YES;
    
    NSDictionary *temDic = dict;
    //获取token
    NSString *token = [temDic objectForKey:@"token"];
    if (!ISEmptyString(token)) {
        [XLUserDefaults saveLastAvailableToken:token];
    }
    [XLNetworking updateCustomHeaderDicWithToken:token];
        
    //存储用户信息
    XLUserInfo *model = [XLUserInfo mj_objectWithKeyValues:dict];
    model.isStaff = isStaff;
    [[XLUserData sharedXLUserData] saveCurrentUserInfo:model];
        
    [XLNotif postNotificationName:XLNotificationUserLoginSuccess object:nil];
    
    //前往首页
    [[AppStartUpConfig sharedAppStartUpConfig] jumpMainControllerWithVC:(XLBaseViewController *)viewC];
    
}





@end
